package com.ilop.sthome.ui.fragment.message;

import android.util.Log;

import com.example.common.base.BasePresentFragment;
import com.example.common.utils.LiveDataBus;
import com.example.common.view.refresh.CustomRefreshView;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.bean.DeviceHistoryBean;
import com.ilop.sthome.data.event.EventRefreshLogs;
import com.ilop.sthome.mvp.contract.message.AlarmLogContract;
import com.ilop.sthome.mvp.present.message.AlarmLogPresenter;
import com.ilop.sthome.ui.adapter.device.DeviceHistoryAdapter;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.FragmentAlarmLogBinding;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author skygge
 * @date 2019-12-31.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：网关消息页面
 */
public class AlarmLogFragment extends BasePresentFragment<AlarmLogPresenter, FragmentAlarmLogBinding> implements AlarmLogContract.IView {

    private int mPage = 1;
    private boolean isEnd = false;
    private boolean isReceive = true;
    private DeviceHistoryAdapter mAdapter;
    private ExecutorService mExecutorService;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_alarm_log;
    }

    @Override
    protected void initPresent() {
        super.initPresent();
        mPresenter = new AlarmLogPresenter(mContext);
    }

    @Override
    protected void initView() {
        super.initView();
        mExecutorService = Executors.newSingleThreadExecutor();
        mAdapter = new DeviceHistoryAdapter(mContext);
        mDBind.mainMessage.setLoadMoreEnable(true);
        mDBind.mainMessage.setAdapter(mAdapter);
        withoutData();
    }

    @Override
    protected void initData() {
        super.initData();
        mPresenter.sendGatewaySyn(1, false);
    }

    @Override
    protected void initDataBus() {
        super.initDataBus();
        LiveDataBus.get().with(CommonId.REFRESH_ALARM, Integer.class).observe(this, integer -> {
            assert integer!=null;
            Log.i(TAG, "initDataBus: refresh alarm" + integer);
            if (integer == 1){
                mPresenter.refreshView();
            }else {
                mPresenter.sendGatewaySyn(1, true);
            }
        });

        /*LiveDataBus.get().with(CommonId.ALARM_CLICK, WarnBean.class).observe(this, warnBean -> {
            Log.i(TAG, "initDataBus: alarm item click");
            Intent intent = new Intent(mContext, DeviceMessageActivity.class);
            intent.putExtra(CommonId.KEY_MESSAGE, warnBean);
            mContext.startActivity(intent);
        });*/

        LiveDataBus.get().with(CommonId.KEY_EVENT_REFRESH_LOGS, EventRefreshLogs.class).observe(this, event -> {
            dismissProgressDialog();
            if (event != null){
                isReceive = true;
                if (event.getIsOver() == 1) {
                    mPage = 1;
                    isEnd = true;
                } else {
                    mPage = event.getPage() + 1;
                    isEnd = false;
                }
                if (event.getAlarmLogs()!=null) {
                    Runnable runnable = ()-> mPresenter.getAlarmLogs(event.getAlarmLogs());
                    mExecutorService.execute(runnable);
                }
            }
        });
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.mainMessage.setOnLoadListener(new CustomRefreshView.OnLoadListener() {
            @Override
            public void onRefresh() {
                if (isReceive) {
                    mPresenter.sendGatewaySyn(1, true);
                    isReceive = false;
                }else {
                    showToast(getString(R.string.please_not_operation_so_quickly));
                }
                mDBind.mainMessage.complete();
            }

            @Override
            public void onLoadMore() {
                if (!isEnd) mPresenter.sendGatewaySyn(mPage, isReceive);
                mDBind.mainMessage.complete();
                mDBind.mainMessage.onLoadingMore();
            }
        });
    }

    @Override
    public void showHistory(List<DeviceHistoryBean> history) {
        mAdapter.setList(history);
        mDBind.mainMessage.onNoMore();
    }

    @Override
    public void withoutData() {
        mAdapter.setList(null);
        mDBind.mainMessage.setEmptyView(getString(R.string.no_data), R.mipmap.device_history_empty);
    }

    @Override
    public void withoutGateway() {
        mAdapter.setList(null);
        mDBind.mainMessage.setEmptyView(getString(R.string.please_add_gateway_first), R.mipmap.device_history_empty);
    }

    @Override
    public void showProgress() {
        showProgressDialog();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mExecutorService!=null && !mExecutorService.isShutdown()){
            mExecutorService.isShutdown();
        }
    }
}